<?php
/**
 * @file
 */
function ffs_admin_content_types($form, &$form_state) {
  $ffs_entity = variable_get('ffs_content_types', NULL);
  $weight = 0;
  $form['ffs_content_types'] = array('#tree' => TRUE,);
  foreach (node_type_get_types() as $type) {
    $form['ffs_content_types'][$type->type] = array(
      'label' => array('#markup' => $type->name),
      'weight' => array(
        '#type' => 'weight',
        '#title' => t('Weight for @title', array('@title' => $type->name)),
        '#title_display' => 'invisible',
        '#default_value' => !empty($ffs_entity[$type->type]['weight']) ? $ffs_entity[$type->type]['weight'] : $weight++,
      ),
      'menu' => array(
        '#type' => 'checkbox',
        '#default_value' => !empty($ffs_entity[$type->type]['menu']) ? $ffs_entity[$type->type]['menu'] : FALSE
      ),
      'name' => array(
        '#type' => 'value',
        '#value' => $type->name
      ),
      'entity_type' => array(
        '#type' => 'value',
        '#value' => 'node'
      )
    );
  }
  $form['#theme'] = 'ffs_admin_content_types';

  return system_settings_form($form);
}

/**
 * Returns HTML for the text format administration overview form.
 *
 * @param $variables
 *   An associative array containing:
 *   - form: A render element representing the form.
 *
 * @ingroup themeable
 */
function theme_ffs_admin_content_types($variables) {
  $form = $variables['form'];
  $rows = array();
  foreach (element_children($form['ffs_content_types']) as $id) {
    $form['ffs_content_types'][$id]['weight']['#attributes']['class'] = array('text-format-order-weight');
    $rows[] = array(
      'data' => array(
        drupal_render($form['ffs_content_types'][$id]['label']),
        drupal_render($form['ffs_content_types'][$id]['weight']),
        drupal_render($form['ffs_content_types'][$id]['menu']),
      ),
      'class' => array('draggable'),
    );
  }
  $header = array(t('Name'), t('Weight'), t('Active Menu'));
  $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'text-format-order')));
  $output .= drupal_render_children($form);

  drupal_add_tabledrag('text-format-order', 'order', 'sibling', 'text-format-order-weight');

  return $output;
}
